package com.itheima.boot.controller;

import com.itheima.boot.domain.Book;
import com.itheima.boot.domain.R;
import com.itheima.boot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    BookService bookService;

    @GetMapping
    public R<List> getAll(@RequestParam(required = false) Integer status,
                           @RequestParam(required = false) String bookName){
        Book book=new Book();
        book.setStatus(status);
        book.setBookName(bookName);
        List<Book> list = bookService.getAll(book);
        return R.success(list);
    }

    @PostMapping
    public  R<String> add(@RequestBody Book book){
        bookService.add(book);
        return  R.success("添加成功");
    }

    @DeleteMapping("/{id}")
    public  R<String> delete(@PathVariable Integer id){
        bookService.delete(id);
        return  R.success("删除成功");
    }

    @GetMapping("/{id}")
    public  R<Book> selectById(@PathVariable Integer id){
        Book book=bookService.selectById(id);
        return R.success(book);
    }

    @PutMapping
    public  R<String> update(@RequestBody Book book){
        bookService.update(book);
        return R.success("修改成功");
    }
}
